﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.Xml;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Html
{
    public class Form : IParser, IDesignerInitialization
    {

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            return new HtmlResult()
                .SetTag("form")
                .AddAttribute("method", "post");
        }

        public string Name
        {
            get { return "Form"; }
        }

        public string Namespace
        {
            get { return "Html";  }
        }

        public void Design(IDesigner designer)
        {
            designer.AddControl("Html:Form")
                .AddContentBeforeChildren("<form><span>Form</span>")
                .AddContentAfterChildren("</form>");
        }

        public string AddToTemplate(IXmlElement input, string initializer)
        {
            //
            //  ok, how we add ourself to the template is pretty easy really
            //  just wrap anything with a form element
            if ((initializer ?? "").ToLower() == "wrap selection")
                return "<html:form method=\"post\">" + input.OuterXml + "</html:form>";
            else
                return ControlInitializerHelper.AddToTemplate("<html:form method=\"post\"></html:form>", input, initializer);

        }

        public string [] GetInitializers()
        {
            return ControlInitializerHelper.GetInitializers();
        }
    }
}
